package com.weiaibenpao.demo.chislim.hurui.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.bean.Li_UserBeanResult;
import com.weiaibenpao.demo.chislim.bean.UserBean;
import com.weiaibenpao.demo.chislim.model.MyModel;
import com.weiaibenpao.demo.chislim.util.Default;
import com.weiaibenpao.demo.chislim.util.WriteReadSharedPrefs;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.weiaibenpao.demo.chislim.bean.UserBean.getUserBean;

public class UserInfoActivity extends AppCompatActivity {


    @BindView(R.id.et_nick)
    EditText etNick;
    @BindView(R.id.tv_limit)
    TextView tvLimit;

    private int type;
    private String nick;
    private Map<String,Object> map;
    private UserBean userBean;
    private Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info_acitvity);
        ButterKnife.bind(this);
        map=new HashMap<>();
         context=UserInfoActivity.this;
        type = getIntent().getIntExtra(Default.TYPE,1);
        nick = getIntent().getStringExtra(Default.NICK);
        Log.e("wlx33", "nick: "+nick );
        initView();
    }

    private void initView() {
        if (type == 1) {
            etNick.setHint("请输入新的昵称");
            tvLimit.setText("最多输入20个字符，不允许特殊字符");

        } else {
            etNick.setHint("请输入新的个性签名");
            tvLimit.setText("最多输入30个字符，不允许特殊字符");

        }
        userBean = getUserBean();
        if (!TextUtils.isEmpty(nick)) {
            etNick.setText(nick);
        }

        etNick.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
             if (type==1){
                 if (s.length()>20) {
                     etNick.setText(s.delete(20, s.length()));
                 }
             }else {
                 if (s.length()>30) {
                     etNick.setText(s.delete(30, s.length()));
                 }
             }
            }
        });
    }

    @OnClick({R.id.btn_commit,R.id.back})
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_commit:
                if (!TextUtils.isEmpty(etNick.getText().toString())){
                   if (type==1){
                       userBean.userName=etNick.getText().toString();
                       updateUser(userBean.userId+"","",userBean.userName,"",0,0,"","","");

                   }else {
                       userBean.userTntru=etNick.getText().toString();
                       updateUser(userBean.userId+"","","","",0,0,"","",userBean.userTntru);

                   }
                }
                break;
            case R.id.back:
                finish();
                break;
        }
    }
    /**
     * 修改用户信息
     */
    public void updateUser(String id,String userImage,String userName,String userSex,int userHeigh,float userWeight,
                           String userBirth,String userPhone,String userIntru){

        Call<Li_UserBeanResult> call = MyModel.getModel().getService().updateUserInfo(id,userImage,userSex,userName,userHeigh,userWeight,
                userBirth,userPhone,userIntru);

        call.enqueue(new Callback<Li_UserBeanResult>() {
            @Override
            public void onResponse(Call<Li_UserBeanResult> call, Response<Li_UserBeanResult> response) {
                if (response.isSuccessful()) {
                    Li_UserBeanResult result = response.body();
                    if (result.getCode() == 0) {
                        Toast.makeText(context,"修改成功",Toast.LENGTH_SHORT).show();
                        WriteReadSharedPrefs.writeUser(context,result.getData());
                        WriteReadSharedPrefs.readUser(context, getUserBean());
                        Log.e("wlx000", "onResponse: "+ result.getData().toString());
                        EventBus.getDefault().post(true);

                    }
                }else{
                    Toast.makeText(context,"fail",Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onFailure(Call<Li_UserBeanResult> call, Throwable t) {
                Toast.makeText(context,t.getMessage(),Toast.LENGTH_SHORT).show();
            }
        });
        finish();
    }
}
